import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/analysis/provider/analysis_provider.dart';
import 'package:boss/ui/widget/shadow_radius_card.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flustars/flustars.dart' as flustars;

import '../tooltip_icon.dart';

class RevenueOfferTable extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<AnalysisProvider>(
      builder: (context, provider, _) => DefaultTextStyle(
        style: TextStyleConstant().normal_1_28,
        child: ShadowRadiusCard(
          padding: EdgeInsets.symmetric(
            horizontal: 10.w,
          ),
          child: Column(
            children: <Widget>[
              Container(
                height: 80.w,
                padding: EdgeInsets.symmetric(
                  horizontal: 10.w,
                ),
                alignment: Alignment.center,
                child: Row(
                  children: <Widget>[
                    Text(
                      '优惠指标',
                      style: TextStyleConstant().normal_1_34,
                    ),
                    ToolTipIcon('各优惠指标占比=（各优惠金额/总原价金额）*100%'),
                    SizedBox(
                      width: 20.w,
                    ),
                    Text(
                      '不含外卖',
                      style: TextStyleConstant().normal_3_20,
                    )
                  ],
                ),
              ),
              HeaderRow(
                title: '优惠合计',
                price: provider.storeRevenue.totalDiscount,
                percentage: provider.storeRevenue.totalDiscountPercent,
                isHeader: true,
              ),
              HeaderRow(
                title: '订单优惠金额',
                price: provider.storeRevenue.orderDiscount,
                percentage: provider.storeRevenue.orderDiscountPercent,
              ),
              HeaderRow(
                title: '菜品优惠金额',
                price: provider.storeRevenue.commodityDiscount,
                percentage: provider.storeRevenue.commodityDiscountPercent,
              ),
              HeaderRow(
                title: '会员优惠金额',
                price: provider.storeRevenue.memberDecreaseAmount,
                percentage: provider.storeRevenue.memberDecreasePercent,
              ),
              HeaderRow(
                title: '券类优惠金额',
                price: provider.storeRevenue.couponDiscount,
                percentage: provider.storeRevenue.couponDiscountPercent,
              ),
              HeaderRow(
                title: '免单优惠金额',
                price: provider.storeRevenue.freeAmount,
                percentage: provider.storeRevenue.freeAmountPercent,
              ),
              HeaderRow(
                title: '抹零金额',
                price: provider.storeRevenue.decreaseTotalAmount,
                percentage: provider.storeRevenue.decreaseTotalAmountPercent,
                isHeader: true,
              ),
              HeaderRow(
                title: '系统抹零金额',
                price: provider.storeRevenue.sysDecreaseAmount,
                percentage: provider.storeRevenue.sysDecreaseAmountPercent,
              ),
              HeaderRow(
                title: '减免金额',
                price: provider.storeRevenue.decreaseAmount,
                percentage: provider.storeRevenue.decreaseAmountPercent,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class HeaderRow extends StatelessWidget {
  final String title;
  final int price;
  final String percentage;
  final bool isHeader;

  const HeaderRow({
    Key key,
    this.title,
    this.price,
    this.percentage,
    this.isHeader = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DefaultTextStyle(
      style: isHeader ? TextStyleConstant().normal_1_28 : TextStyleConstant().normal_1_24,
      child: Container(
        height: 80.w,
        color: isHeader ? ColorConstant.ffcc.withOpacity(0.1) : Colors.transparent,
        padding: EdgeInsets.symmetric(
          horizontal: 10.w,
        ),
        child: Row(
          children: <Widget>[
            Expanded(
              child: Text(
                title,
                style: isHeader ? TextStyleConstant().blue_28 : TextStyleConstant().normal_1_28,
              ),
            ),
            Expanded(
              child: Center(
                child: Text(
                  '${AmountUtil.price2String(price)}元',
                  style: isHeader ? TextStyleConstant().blue_28 : TextStyleConstant().normal_1_28,
                ),
              ),
            ),
            Expanded(
              child: Text(
                flustars.TextUtil.isEmpty(percentage) ? '0.00%' : '$percentage%',
                textAlign: TextAlign.end,
                style: isHeader ? TextStyleConstant().blue_28 : TextStyleConstant().normal_1_28,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
